package com.lcl.jarvis.controller;

import com.lcl.jarvis.dto.TbUserAuthDTO;
import com.lcl.jarvis.dto.TbUserAuthQueryDTO;
import com.lcl.jarvis.service.TbUserAuthService;
import com.lcl.jarvis.util.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@Api(value = "/login", tags = {"登录模块"})
public class LoginController {


    private final TbUserAuthService tbUserAuthService;

    @Autowired
    public LoginController(TbUserAuthService tbUserAuthService){
        this.tbUserAuthService = tbUserAuthService;
    }

    @PostMapping("/login")
    public Result login(@RequestBody TbUserAuthQueryDTO tbUserAuthDto) {
        log.info("login");
        if(tbUserAuthDto.getUsername() == null || tbUserAuthDto.getPassword() == null){
            return Result.fail("用户名或密码不能为空");
        }

        TbUserAuthDTO tbUserAuthDTO = tbUserAuthService.queryUserByUsername(tbUserAuthDto);

        if(tbUserAuthDto.getUsername().equals(tbUserAuthDTO.getUsername()) && tbUserAuthDto.getPassword().equals(tbUserAuthDTO.getPassword())){
            tbUserAuthDto.setToken("admintoken");
            return Result.ok(tbUserAuthDto);
        }else{
            throw new RuntimeException("用户名或密码错误");
        }
    }


    @RequestMapping("/logout")
    public Result logout() {
        return Result.ok();
    }
}
